<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true">
      <el-form-item label="企业客户端名称" prop="qyName">
        <el-input v-model="queryParams.qyName" placeholder="请输入企业客户端名称" clearable size="small"
          @keyup.enter.native="handleQuery" maxlength="20" show-word-limit  />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small" @change="handleQuery">
          <el-option label="已启用" value="0" />
          <el-option label="已停用" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8" style="margin-bottom: 20px;">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['qywx:qyClient:add']">新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['qywx:qyClient:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['qywx:qyClient:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['qywx:qyClient:export']"
          >导出</el-button
        >
      </el-col> -->
    </el-row>

    <el-table v-loading="loading" :data="qyClientList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- <el-table-column label="id" align="center" prop="id" /> -->
      <el-table-column type="index" label="序号" width="55" align="center" />
      <el-table-column label="企业客户端名称" align="left" prop="qyName" :show-overflow-tooltip="true"q />
      <el-table-column label="企业ID" align="left" prop="qyId" :show-overflow-tooltip="true" />
      <el-table-column label="密钥" align="left" prop="secret" :show-overflow-tooltip="true" />
      <el-table-column label="状态" align="center" prop="status" width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 0">已启用</el-tag>
          <el-tag type="danger" v-if="scope.row.status == 1">已停用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="150" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['qywx:qyClient:edit']" v-if="scope.row.status == '1'">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-magic-stick" :class="scope.row.status === '0' ? 'delete-btn' : 'start-btn'"
            @click="handleStatusChange(scope.row)">{{ scope.row.status === "0" ? "停用" : "启用" }}</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['qywx:qyClient:remove']" v-if="scope.row.status == '1'" class="delete-btn">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改企业微信客户端对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-form-item label="企业客户端名称" prop="qyName">
          <el-input 
            v-model="form.qyName" 
            placeholder="请输入企业客户端名称" 
            maxlength="20" 
            show-word-limit
            @input="handleQyNameInput" />
        </el-form-item>
        <el-form-item label="企业ID" prop="qyId">
          <el-input 
            v-model="form.qyId" 
            placeholder="请输入企业ID" 
            maxlength="50" 
            show-word-limit
            @input="handleQyIdInput" />
        </el-form-item>
        <el-form-item label="密钥" prop="secret">
          <el-input 
            v-model="form.secret" 
            placeholder="请输入密钥" 
            maxlength="50" 
            show-word-limit
            @input="handleSecretInput" />
        </el-form-item>
        <!-- <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio label="0">启用</el-radio>
            <el-radio label="1">停用</el-radio>
          </el-radio-group>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">发 布</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listQyClient,
  getQyClient,
  delQyClient,
  addQyClient,
  updateQyClient,
  exportQyClient,
  updateQyClientStatus
} from "@/api/qywx/qyClient";

export default {
  name: "",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 企业微信客户端表格数据
      qyClientList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        qyName: undefined,
        qyId: undefined,
        secret: undefined,
        status: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        qyName: [
          { required: true, message: "企业客户端名称不能为空", trigger: "blur" },
          { pattern: /^[\u4E00-\u9FA5]+$/, message: "企业客户端名称只能输入中文", trigger: "blur" }
        ],
        qyId: [
          { required: true, message: "企业ID不能为空", trigger: "blur" },
          { pattern: /^[a-zA-Z0-9]+$/, message: "企业ID只能输入英文和数字", trigger: "blur" }
        ],
        secret: [
          { required: true, message: "密钥不能为空", trigger: "blur" },
          { pattern: /^[a-zA-Z0-9]+$/, message: "密钥只能输入英文和数字", trigger: "blur" }
        ]
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询企业微信客户端列表 */
    getList() {
      this.loading = true;
      listQyClient(this.queryParams).then((response) => {
        this.qyClientList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        qyName: undefined,
        qyId: undefined,
        secret: undefined,
        delFlag: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        status: "0",
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增企业微信客户端";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getQyClient(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改企业微信客户端";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            updateQyClient(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addQyClient(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除企业微信客户端编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        },
      )
        .then(function () {
          return delQyClient(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function () { });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有企业微信客户端数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportQyClient(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        })
        .catch(function () { });
    },
    // 状态修改
    handleStatusChange(row) {
      let text = row.status === "0" ? "停用" : "启用";
      let newStatus = row.status === "0" ? "1" : "0";
      this.$confirm('确认' + text + '此数据？').then(function () {
        return updateQyClientStatus({
          id: row.id,
          status: newStatus
        });
      }).then(() => {
        this.msgSuccess("已" + text);
        this.getList();
      }).catch(() => { });
    },
    // 企业客户端名称只能输入中文
    handleQyNameInput(val) {
      this.form.qyName = val.replace(/[^\u4E00-\u9FA5]/g, '');
    },
    // 企业ID只能输入英文和数字
    handleQyIdInput(val) {
      this.form.qyId = val.replace(/[^a-zA-Z0-9]/g, '');
    },
    // 密钥只能输入英文和数字
    handleSecretInput(val) {
      this.form.secret = val.replace(/[^a-zA-Z0-9]/g, '');
    },
  },
};
</script>

<style lang="scss" scoped>
.avatar-uploader i {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}


.stop-btn {
  color: #ff9900 !important;
}

.delete-btn {
  color: #ff4949 !important;
}
</style>
